<script>
import { mapGetters } from 'vuex'
import {
	login
} from '@/api'
import store from './store/store'
export default {
	onLaunch: function() {
		console.log('App Launch')
		uni.hideTabBar()
		uni.getNetworkType({
			success: ({ networkType }) => {
				if(networkType == 'none') {
					uni.reLaunch({
						url: '/pages/network/index'
					})
				}
				return
			},
			false: () => {
				uni.reLaunch({
					url: '/pages/network/index'
				})
				return
			}
		})
		if((store.getters.username ?? '') == '') {
			uni.reLaunch({
				url: '/pages/login'
			})
			return
		}
		// APP启动自动登陆
		this.autoLogin()
		// uni.getStorage({
		// 	key: 'info',
		// 	success: (data) => {
		// 		if(data) {
		// 			this.$store.dispatch('storeUserInfo', data.data)
		// 			this.$store.dispatch('storeStudentInfo', data.data.student)
		// 		}
		// 	}
		// })
		// uni.getStorage({
		// 	key: 'token',
		// 	success: (data) => {
		// 		if(data && data.data) {
		// 			this.$store.dispatch('storeToken', data.data)
		// 			// uni.reLaunch({
		// 			// 	url: '/pages/index/index'
		// 			// })
		// 		}else {
		// 			uni.reLaunch({
		// 				url: '/pages/login'
		// 			})
		// 		}
		// 	}
		// })
	},
	onShow: function() {
		console.log('App Show')
	},
	onHide: function() {
		console.log('App Hide')
		uni.hideLoading()
	},
	computed: {
		...mapGetters(['username', 'password'])
	},
	methods: {
		autoLogin() {
			// #ifdef H5
			if(this.$route.fullPath.indexOf('login') != -1) {
				return
			}
			// #endif
			if((this.username ?? '') == '' || (this.password ?? '') == '') {
				uni.reLaunch({
					url: '/pages/login?type=loginExpire'
				})
				return
			}
			const { username, password } = this
			login({
				username,
				password,
				os: 'app'
			}).then(res => {
				this.$loading.hide()
				if (res.data.code === 200) {
					this.$store.dispatch('storeToken', res.header.token)
					this.$appStore.setStorage('token', res.header.token)
					const obj = res.data.data
					obj.password = password
					this.$store.dispatch('storeUserInfo', obj)
					// uni.reLaunch({
					// 	url: '/pages/index'
					// })
				} else {
					this.$toast.show({msg: res.data.msg})
				}
			})
		}
	}
}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	@import './uni.scss';

	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
		color: #333333;
		font-size: 14px;
		font-family: 'PingFang-SC-Medium';
	}

	.mask {
		background-color: rgba(0,0,0,.3);
	}

	/* #endif */
	html,
	body,
	h1,
	h2,
	h3,
	h4,
	h5,
	h6 {
		padding: 0;
		margin: 0;
	}

	// #ifdef H5
	*::-webkit-scrollbar {
		display: none;
		width: 0;
		height: 0;
		opacity: 0;
	}

	// #endif

	.container {
		width: 100vw;
		height: 100vh;
		overflow: hidden;
	}

	.content {
		width: 100%;
		height: calc(100vh - 44px - var(--status-bar-height));
		margin: 0;
		overflow-y: auto;
		z-index: -1;

	}

	.has-bg::v-deep .u-navbar__content {
		background-color: #0aa679 !important;

		.u-navbar__content__left {
			color: #fff !important;
		}

		.uicon-arrow-left {
			color: #fff !important;
		}

		.u-navbar__content__title {
			color: #fff !important;
		}

		.u-navbar__content__right {
			color: #fff !important;
		}
	}

	button::after {
		border: none;
	}

	.button-hover {
		color: unset;
		background: inherit;
	}

	.buttons {
		display: flex;
		width: 100%;

		.button {
			width: 100%;
			border-radius: 4px;
			padding: 6px 0;
			margin: 0 10px 5px;
			line-height: 20px;
			font-size: 14px;
			text-align: center;
		}

		.button-primary {
			background-color: #0aa679;
			color: #fff;
		}
	}

	.white {
		color: #ffffff;
	}

	.submitBtn {
		width: auto;
		margin: 15px 10px;
		background-color: $primary-theme-color;
		padding: 10px 15px;
		box-sizing: border-box;
		border-radius: 4px;
		color: #ffffff;
		text-align: center;
	}
</style>
